;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var css_add = require("./permisionGroupAdd.css");
	var router = require("../hash/pageRouter")
	var roleId;
	var role_array = [ {
		id : "",
		name : "首页",
		sub : [ {
			id : "priceWarning_1",
			name : "价格波动提醒"
		}, {
			id : "waitWork_1",
			name : "待办事项"
		}, {
			id : "financingApprove_1",
			name : "融资审批"
		},  {
			id: "financeApproveButton_1",
			name: "融资审批同意/拒绝"
		},  {
			id: "businessAudit_1",
			name: "业务审核"
		},  {
			id: "businessManageAudit_1",
			name: "业务领导审核"
		},  {
			id: "riskAudit_1",
			name: "风控审核"
		},  {
			id: "riskMemberAudit_1",
			name: "风控领导审核"
		},  {
			id: "operationAudit_1",
			name: "运营审核"
		},  {
			id: "operationManageAudit_1",
			name: "运营领导审核"
		},  {
			id: "financialAudit_1",
			name: "资金审核"
		},  {
			id: "financialManageAudit_1",
			name: "资金领导审核"
		},  {
			id: "ceoAudit_1",
			name: "总经理审核"
		},  {
			id: "finalAudit_1",
			name: "执行董事审核"
		}, {
			id : "contractUnsign_1",
			name : "合同管理"
		}, {
			id : "financeContractManage_1",
			name : "合同上传"
		},  {
			id : "financeContractSubmit_1",
			name : "合同提交"
		}, {
			id : "financeCallBack_1",
			name : "合同打回"
		}, {
			id : "financeSample_1",
			name : "合同范本"
		}, {
			id : "financeContractConfirm_1",
			name : "合同确认"
		}, {
			id : "noticeReceive_1",
			name : "通知装货"
		}, {
			id : "financeUnsupervise_1",
			name : "确认监管"
		}, {
			id : "financeUnrelease_1",
			name : "放款申请"
		}, {
			id : "financeUnloan_1",
			name : "放款审批"
		}, {
			id : "loanNoticeAudit1_1",
			name : "放款审批-运营审核"
		}, {
			id : "loanNoticeAudit2_1",
			name : "放款审批-运营领导审核"
		}, {
			id : "loanNoticeBusinessAudit_1",
			name : "放款审批-风控审核"
		}, {
			id : "loanNoticeOperationAudit_1",
			name : "放款审批-风控领导审核"
		}, {
			id : "loanNoticeFinancialAudit_1",
			name : "放款审批-资金审核"
		}, {
			id : "loanNoticeFinancialLeaderAudit_1",
			name : "放款审批-资金领导审核"
		}, {
			id : "loanNoticeLeader2Audit_1",
			name : "放款审批-总经理审核"
		}, {
			id : "loanNoticeLeader1Audit_1",
			name : "放款审批-执行董事审核"
		}, {
			id : "loanNoticeConsent_1",
			name : "放款审批-同意"
		}, {
			id : "financeUntransport_1",
			name : "通知运输"
		}, {
			id : "financePickupExam_1",
			name : "提货审批"
		}, {
			id : "pickupConsentBusinessAudit_1",
			name : "提货审批-运营审核"
		}, {
			id : "pickupConsentAudit1_1",
			name : "提货审批-运营领导审核"
		}, {
			id : "pickupConsentRiskAudit_1",
			name : "提货审批-资金审核"
		}, {
			id : "pickupConsentRiskMemberAudit_1",
			name : "提货审批-资金领导审核"
		}, {
			id : "pickupConsentFinancialAudit_1",
			name : "提货审批-风控审核"
		}, {
			id : "pickupConsentOperationAudit_1",
			name : "提货审批-风控领导审核"
		}, {
			id : "pickupConsentCeoAudit_1",
			name : "提货审批-总经理审核"
		}, {
			id : "pickupConsentFinalAudit_1",
			name : "提货审批-执行董事审核"
		}, {
			id : "pickupConsentConsent_1",
			name : "提货审批-审批通过"
		}, {
			id : "financeUnreceive_1",
			name : "收款确认"
		}, {
			id : "financeUnreceiveBulkstock_1",
			name : "放货通知"
		}, {
			id : "cancelSupervise_1",
			name : "监管解除"
		}, {
			id : "exceptionHandle_1",
			name : "异常处置"
		}, {
			id : "pickupPre_1",
			name : "提前还款"
		}, {
			id : "pickupDelay_1",
			name : "延期还款"
		}, {
			id : "interestDelay_1",
			name : "延期计息"
		}, {
			id : "detained_1",
			name : "扣货申请"
		}, {
			id : "pickupRansom_1",
			name : "赎货申请"
		}, {
			id : "badassetsHandle_1",
			name : "不良资产"
		}, {
			id : "abnormalManage_1",
			name : "异常管理"
		} ]
	}, {
		id : "",
		name : "融资项目：",
		sub : [ {
			id : "financeApplicationItem_1",
			name : "融资项目"
		}, {
			id : "financeApplicationManage_1",
			name : "融资申请"
		}, {
			id : "bailAddition_1",
			name : "保证金追加"
		}, {
			id : "financeBookingSpace_1",
			name : "融资订舱"
		}, {
			id : "replenish_1",
			name : "补货"
		}, {
			id : "phase_1",
			name : "收息"
		}, {
			id : "endFinanceList_1",
			name : "已完成融资"
		}, {
			id : "recieveNoticeList_1",
			name : "收货通知"
		}, {
			id : "transportNoticeList_1",
			name : "运输通知"
		}, {
			id : "deliveryNoticeList_1",
			name : "放货通知"
		}, {
			id : "loanNoticeList_1",
			name : "放款单确认"
		}, {
			id : "auditBailAddition_1",
			name : "追加保证金审核"
		}, {
			id : "replenishAudit_1 ",
			name : "补货审核"
		}, {
			id : "bailReturnAudit_1",
			name : "保证金退还审核"
		}, {
			id : "replenishConfirm_1",
			name : "补货确认"
		}, {
			id : "constractCallBackInfo_1",
			name : "打回记录"
		}, {
			id : "accountMoneyStatement_1",
			name : "应收应付管理"
		}, {
			id : "receiptManage_1",
			name : "应收管理"
		}, {
			id : "disbursementManage_1",
			name : "应付管理"
		}, {
			id : "receiptConfirmManage_1 ",
			name : "应收确认"
		}, {
			id : "verifyList_1",
			name : "收付款单"
		}, {
			id : "verifyReceiptList_1",
			name : "收款单"
		}, {
			id : "verifyDisbursementList_1",
			name : "付款单"
		} ]
	}, {
		id : "",
		name : "客户管理：",
		sub : [ {
			id : "enterpriseManage_1",
			name : "企业用户管理"
		}, {
			id : "clientManage_1",
			name : "企业用户管理"
		}, {
			id : "memberGradeAduitList_1",
			name : "用户等级审核"
		}, {
			id : "enterpriseQualification_1",
			name : "用户资质审核"
		}, {
			id : "memberGradeAdjust_1",
			name : "资信等级调整"
		}, {
			id : "logisticsUserManage_1",
			name : "物流公司管理"
		}, {
			id : "distributors_1",
			name : "分销商管理"
		} ]
	}, {
		id : "",
		name : "大宗商品：",
		sub : [ {
			id : "bulkstockCategoryManage_1",
			name : "商品类别管理"
		}, {
			id : "bulkstockManage_1",
			name : "大宗商品管理"
		}, {
			id : "bulkstockPriceCollection_1",
			name : "商品价格采集"
		}, {
			id : "priceAcerageManage_1",
			name : "商品均价汇总"
		}, {
			id : "priceChecked_1",
			name : "汇总价格已审核发布"
		}, {
			id : "priceUnchencked_1",
			name : "汇总价格待审核"
		} ]
	}, {
		id : "",
		name : "新闻资讯：",
		sub : [ {
			id : "newsInformationManage_1",
			name : "新闻资讯管理"
		}, {
			id : "newsBannerManage_1",
			name : "栏目管理"
		} ]
	}, {
		id : "",
		name : "异常管理",
		sub : [ {
			id : "exceptionManages_1",
			name : "异常管理"
		}, {
			id : "exceptionUnprocess_1",
			name : "未处理异常"
		}, {
			id : "exceptionAccepted_1",
			name : "已接受异常"
		}, {
			id : "exceptionProcessing_1",
			name : "调查中异常"
		}, {
			id : "exceptionCompleted_1",
			name : "处理完毕异常"
		}, {
			id : "exceptionHandles_1",
			name : "异常处置"
		}, {
			id : "pickupPreA_1",
			name : "提前还款处理"
		}, {
			id : "pickupDelayA_1",
			name : "延期还款处理"
		}, {
			id : "interestDelayA_1",
			name : "延期计息处理"
		}, {
			id : "detainedA_1",
			name : "扣货申请处理"
		}, {
			id : "pickupRansomA_1",
			name : "赎货申请处理"
		}, {
			id : "badassetsHandleA_1",
			name : "不良资产处理"
		} ]
	}, {
		id : "",
		name : "保险管理：",
		sub : [ {
			id : "insuranceManages_1",
			name : "保险管理"
		}, {
			id : "insurance1_1",
			name : "保险待审核"
		}, {
			id : "insurance2_1",
			name : "保险已生效"
		}, {
			id : "insurance3_1",
			name : "保险已撤销"
		}, {
			id : "insurance4_1",
			name : "保险已拒绝"
		}, {
			id : "claimManage_1",
			name : "理赔管理"
		} ]
	}, {
		id : "",
		name : "系统管理：",
		sub : [ {
			id : "orgaizationManage_1",
			name : "组织管理"
		}, {
			id : "permisionGroupManage_1",
			name : "角色权限"
		}, {
			id : "announceManage_1",
			name : "公告管理"
		}, {
			id : "announceAudit_1",
			name : "公告审核"
		}, {
			id : "advertisementManage_1",
			name : "广告管理"
		}, {
			id : "memberGradeManage_1",
			name : "会员等级管理"
		}, {
			id : "memberGradeEdit_1",
			name : "会员等级编辑"
		}, {
			id : "memberGradeEdit_2",
			name : "费用信息编辑"
		}, {
			id : "memberGradeView_1",
			name : "会员等级查看"
		}, {
			id : "systemSettings_1",
			name : "系统设置"
		}, {
			id : "systemParamManage_1",
			name : "系统参数管理"
		}, {
			id : "contractTemplateManage_1",
			name : "合同范本管理"
		}, {
			id : "systemParamManage_2",
			name : "费用信息"
		}, {
			id : "blankInfoManager_1",
			name : "账户管理"
		}, {
			id : "lendUintManager_1",
			name : "出借单位"
		}, {
			id : "productManage_1",
			name : "产品管理"
		}, {
			id : "productAdd_1",
			name : "产品添加"
		}, {
			id : "productPublished_1",
			name : "产品已发布"
		}, {
			id : "productUnSubmit_1",
			name : "产品待提交"
		}, {
			id : "productUnExam_1",
			name : "产品待审核"
		}, {
			id : "productUnPublish_1",
			name : "产品待发布"
		}, {
			id : "productStoped_1",
			name : "产品已停用"
		}, {
			id : "constractManages_1",
			name : "合同管理"
		}, {
			id : "constractLogisticsManage_1",
			name : "物流合同"
		}, {
			id : "logisticsConstractAdd_1",
			name : "物流合同添加"
		}, {
			id : "logisticsConstractPublished_1",
			name : "物流合同已发布"
		}, {
			id : "logisticsConstractUnExam_1",
			name : "物流合同待审核"
		}, {
			id : "logisticsConstractUnPublish_1",
			name : "物流合同待发布"
		}, {
			id : "logisticsConstractUnSubmit_1",
			name : "物流合同待提交"
		}, {
			id : "logisticsConstractStoped_1",
			name : "物流合同已停用"
		}, {
			id : "constractSupervisionManage_1",
			name : "监管合同"
		}, {
			id : "supervisionConstractAdd_1",
			name : "监管合同添加"
		}, {
			id : "supervisionConstractPublished_1",
			name : "监管合同已发布"
		}, {
			id : "supervisionConstractUnExam_1",
			name : "监管合同待审核"
		}, {
			id : "supervisionConstractUnPublish_1",
			name : "监管合同待发布"
		}, {
			id : "supervisionConstractUnSubmit_1",
			name : "监管合同待提交"
		}, {
			id : "supervisionConstractStoped_1",
			name : "监管合同已停用"
		}, {
			id : "useCardTypeManager_1",
			name : "卡类管理"
		}, {
			id : "useCardManager_1",
			name : "用户卡管理"
		} ]
	}, {
		id : "",
		name : "预警中心：",
		sub : [ {
			id : "warningSchemeManage_1",
			name : "预警管理"
		}, {
			id : "warningScheme_1",
			name : "预警方案"
		}, {
			id : "auditWarning_1",
			name : "时效设定"
		}, {
			id : "warningInfo_1",
			name : "预警信息"
		}, {
			id : "schemeResult_1",
			name : "融资预警"
		}, {
			id : "riskWarning_1",
			name : "风险预警"
		}, {
			id : "warningHistory_1",
			name : "预警历史"
		}, {
			id : "warningSchemeResultSettled_1",
			name : "融资预警"
		}, {
			id : "riskWarningSettled_1",
			name : "风险预警"
		} ]
	}, {
		id : "",
		name : "导航栏",
		sub : [ {
			id : "mainPage_11",
			name : "首页"
		}, {
			id : "financingManage_11",
			name : "融资项目"
		}, {
			id : "clientManage_11",
			name : "客户管理"
		}, {
			id : "bulkstockManage_11",
			name : "大宗商品"
		}, {
			id : "newsManage_11",
			name : "新闻资讯"
		}, {
			id : "exceptionManage_11",
			name : "异常管理"
		}, {
			id : "insuranceManage_11",
			name : "保险管理"
		}, {
			id : "systemManage_11",
			name : "系统管理"
		}, {
			id : "warningCenter_11",
			name : "预警中心"
		} ]
	} ];
	var init = function(page) {
		if (page.params.roleId != undefined) {
			roleId = page.params.roleId;
			$(".column_title a:last").html("编辑权限组");
			$("#but_confirm").html("完成编辑");
		} else {
			roleId = "";
		}
		permissionsListHandler(role_array);
		initEvent();
	};

	var permissionsListHandler = function(data) {
		var hs = [];
		for (var i = 0; i < data.length; i++) {
			hs.push('<tr class="tr_add">');
			hs.push('<td valign="top" width="14%" class="cell left_cell">');
			hs.push('<span class="check_div">');
			hs.push('<input id="checkAll_' + i
					+ '" class="checkAll_common"  group="' + i
					+ '" name="weiguidang" type="checkbox" value=""/>');
			hs.push('<label for="checkAll_' + i + '" group=group"' + i
					+ '" class="feedCkLabel">' + data[i].name + '</label>');
			hs.push('</span></td>');
			hs.push('<td class="cell right_cell" width="86%">');
			hs.push('<div id="checks_' + i + '">');
			for (var j = 0; j < data[i].sub.length; j++) {
				hs.push('<span class="check_div">');
				hs.push('<input id="listitem_' + i + j + '" name="group' + i
						+ '" name="permissionId" type="checkbox" value="'
						+ data[i].sub[j].id + '"/>');
				hs.push('<label for="listitem_'
								+ i
								+ j
								+ '"   name="group'
								+ i
								+ '" name="permissionId" class="feedCkLabel" permissionId="'
								+ data[i].sub[j].id + '">'
								+ data[i].sub[j].name + '</label>');
				hs.push('</span>');
			}
			;
			hs.push('</div>');
			hs.push('</td>');
		}
		;
		hs.push('</tr>');
		$("#permisiongroup_add_table").html(hs.join(""));
		if (roleId != "") {
			loadExists();
		}
	};

	var initEvent = function() {
		// 修改顶部栏的选中状态
		$(".nav_hover", window.parent.document).removeClass("head_link_1");

		recerseChoice();

		$("#permisiongroup_add_table").on("click", ".feedCkLabel", function() {
			$(this).toggleClass("feedCkChecked");
		});
		$("#permisiongroup_add_table").on(
				"click",
				".checkAll_common",
				function() {
					var group = $(this).attr("group");
					var con = $("#checks_" + group);
					var cks = con.find("[name=group" + group + "]:checkbox");
					if ($(this).is(":checked")) {
						cks.each(function(i) {
							$(cks[i]).prop("checked", true);
							$(cks[i]).siblings(".feedCkLabel").addClass(
									"feedCkChecked");
						});
					} else {
						cks.each(function(i) {
							$(cks[i]).prop("checked", false);
							$(cks[i]).siblings(".feedCkLabel").removeClass(
									"feedCkChecked");
						});
					}
				});

		// 添加权限按钮
		$("#but_confirm").click(function() {
			if (roleId == "") {
				roleAdd();
			} else {
				roleUpdate(roleId);
			}
		});

		// 取消按钮
		$("#but_cancel").click(function() {
			router.route("permisionManage/permisionManage.html");
		});
	};

	// 反选选中框
	var recerseChoice = function() {
		$("#permisiongroup_add_table")
				.on(
						"click",
						"tr.tr_add",
						function() {
							var $this = $(this);
							var cellLabelLeng = $this
									.find(".right_cell .feedCkLabel").length;
							var cellChecked = $this
									.find(".right_cell .feedCkChecked").length;
							if (cellLabelLeng == cellChecked) {
								$this.find(".left_cell .feedCkLabel").addClass(
										"feedCkChecked");
							} else {
								$this.find(".left_cell .feedCkLabel")
										.removeClass("feedCkChecked");
							}
							;
						});
	};

	// 添加权限接口
	var roleAdd = function() {
		var body = {};
		body.permissionId = [];
		$(".check_div .feedCkLabel.feedCkChecked").each(
				function(index, obj) {
					if (!$(obj).attr('permissionId')
							|| $(obj).attr('permissionId').length == 0) {
					} else {
						body.permissionId.push($(obj).attr('permissionId'));
					}
					;
				});
		var role_name = $("#permisiongroup_add_input").val();
		if (role_name.length == 0) {
			util.alert("权限组名称输入不能为空");
			return false;
		}
		;
		if (role_name.length > 255) {
			util.alert("权限组名称长度不能超过255个字符");
			return false;
		}
		;
		body.roleName = role_name;
		base.fnAjax({
			action : "createRole.json",
			async : false, // 同步异步
			nocode : false, // 是否要登陆，true为不用登陆
			oData : body,
			fnSuccess : function(data) {
				if (data.status == 200) {
					$("#but_confirm").unbind();
					router.route("permisionManage/permisionManage.html");
				}
			}
		});
	};
	// 修改权限接口
	var roleUpdate = function(roleId) {
		var body = {};
		body.permissionId = [];
		$(".check_div .feedCkLabel.feedCkChecked").each(
				function(index, obj) {
					if (!$(obj).attr('permissionId')
							|| $(obj).attr('permissionId').length == 0) {
					} else {
						body.permissionId.push($(obj).attr('permissionId'));
					}
					;
				});
		var role_name = $("#permisiongroup_add_input").val();
		if (role_name.length == 0) {
			util.alert("权限组名称输入不能为空");
			return false;
		}
		;
		if (role_name.length > 255) {
			util.alert("权限组名称长度不能超过255个字符");
			return false;
		}
		;
		body.roleName = role_name;
		body.roleId = roleId;
		base.fnAjax({
			action : "updateRole.json",
			async : false, // 同步异步
			nocode : false, // 是否要登陆，true为不用登陆
			oData : body,
			fnSuccess : function(data) {
				if (data.status == 200) {
					$("#but_confirm").unbind();
					router.route("permisionManage/permisionManage.html");
				}
			}
		});
	};

	// 使权限组已有的权限组getChecked
	var loadExists = function() {
		base.fnAjax({
			action : "inquireRoleDetail.json",
			oData : {
				roleId : roleId
			},
			fnSuccess : function(data) {
				var cs = $("#permisiongroup_add_table .feedCkLabel");
				$("#permisiongroup_add_input").val(data.datas[0].roleName);
				$.each(cs, function(idx, itm) {
					var _t = $(itm);
					$.each(data.datas[0].perm, function(ii, tt) {
						if (tt.permissionId == _t.attr("permissionId")) {
							_t.addClass("feedCkChecked");
						}
					});

				});
				$("tr.tr_add").trigger("click");
			}
		});
	};

	return {
		init : init
	};
});